package com.anli.zkconfig;

import com.anli.entity.StockInfo;
import com.anli.service.TestLockService;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMultiLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/8/30  20:10
 * 描述:
 */
@RestController
public class TestZkLock {

   @Autowired(required = false)
   private CuratorFramework curatorFramework;


   @Autowired
   private TestLockService testLockService;

   @RequestMapping("testZk")
   public String testZk() throws Exception {


       InterProcessMultiLock interProcessMultiLock=new InterProcessMultiLock(curatorFramework,Collections.singletonList("/LOCK"));
       //获取锁
       interProcessMultiLock.acquire();

       StockInfo stockInfo=new StockInfo();
       stockInfo.setStock(1L);
       stockInfo.setGoodsId(100L);
       stockInfo.setStockId(2L);
       testLockService.testLock1(stockInfo);

       //释放锁
       interProcessMultiLock.release();


       return "ok";
   }


}
